#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QActionGroup>
#include <QList>
#include "constants.h"
#include "WelcomeScreen/welcomescreen.h"
#include "Editor/texteditor.h"
#include "Editor/formattededitor.h"
#include "Session/createsession.h"
#include "Session/joinsession.h"
#include "Session/opensession.h"
#include "NewFile/newfile.h"
#include "Preferences/preferences.h"
#include "About/about.h"
#include "Statistics/statistics.h"
#include "Manual/manual.h"
#include "Find/find.h"
#include "GoToLine/gotoline.h"
#include "Networking/session.h"
#include "Networking/chatmanager.h"
#include "Networking/documentmanager.h"
#include "Networking/usermanager.h"
#include "Networking/user.h"

namespace Ui
{
	class MainWindow;
}

/**
  * Singleton main window class.
  */
class MainWindow : public QMainWindow
{
	Q_OBJECT

	private:
		static MainWindow *instance;

		Ui::MainWindow *ui;
		WelcomeScreen *welcome;
		NewFile *newFile;
		Preferences *prefs;
		About *about;
		Statistics *stats;
		Manual *manual;
		Find *find;
		GoToLine *goToLine;

		QActionGroup *programmingLanguages;
		QActionGroup *documentMenuList;	//list of documents for the "Documents" menu
		void setIcons();
		void setGeometry();
		void cleanUp();
		QString getNextTabName();
		QString execName;
		Session *session;

	protected:
		MainWindow();

		void moveEvent(QMoveEvent *event);
		void closeEvent(QCloseEvent *event);

	public:
		~MainWindow();

		static MainWindow *getInstance()
		{
			if (!instance)
				instance = new MainWindow();

			return instance;
		}

		Editor* getCurrentEditor();
		QList<Editor*> getEditors() { return session->getDocumentManager()->getDocList(); }
		void setExecutableName(QString name) {execName = name;}		//needed to fork process

	signals:
		void moved();

	public slots:
		void addEditorToTab(Editor *newEd);		//given an editor object, adds it to a new tab
		void hideEditorTab(Editor *ed);
		void restoreEditorTab(Editor *ed);
		void showMessageBox(int ico, QString message);	//shows a message box (used between threads)

		void createSession();
		void joinSession();
		void joinSession(QString addy, int port);
		void leaveSession();
		void quitSession();
		void showWelcome(const bool show = true);
		void hideWelcome();
		void showEditorUiComponents(const bool show = true);
		void createNewFile();
		void openFiles();
		void openFile(const QString fileName);
		void saveFile(const bool saveAs = false);
		void saveFileAs();
		QString getSaveFileName(FILE_TYPE type);
		void closeFile();
		void closeFileTab(const int index);
		void closeAllFiles();
		void pageSetup();
		void print(QPrinter *printer = NULL);
		void printPreview();
		void showPrefs();
		void showAbout();
		void showStats();
		void showManual();
		void showFind(const bool show);
		void showReplace(const bool show);
		void chatSend() const;
		void chatReceive(QString msg) const;
		void currentEditorChanged(int index);
		void updateUserList(int id, QString name, QColor colour, bool enabled);
		void saveSession();
		void loadSession(const QString inputFile = QString());
		void showGoToLine();
		void toggleCutCopyButtons(bool hasSelection);
};

#endif // MAINWINDOW_H
